<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<script type="text/javascript" src="<?php echo STATIC_DOMAIN;?>js/highcharts/js/highcharts.src.js"></script>
<script type="text/javascript" src="<?php echo STATIC_DOMAIN;?>js/highcharts/js/modules/exporting.src.js"></script>
<script type="text/javascript" src="<?php echo STATIC_DOMAIN;?>js/highcharts/js/themes/grid.js"></script>
<script src="../../js/jquery-ui-1.8.12.custom.min.js" type="text/javascript"></script>
<link href="../../skins/version2/jquery-ui-1.8.12.custom.css" type="text/css" rel="stylesheet" />
<style>
.ui-widget {font-family:Arial}
.ui-widget-content{border:none}
.ui-widget-header{background: none;border:none; border-bottom:1px solid #ddd}
.sumaryItem{font:normal 12px arial; line-height:22px;}
.sumaryItem span{font-weight:bold;color:#f00;}
.fieldset{border:1px solid #eee; padding:15px;}
.fieldset legend{font-weight:bold; color:#333}
.fieldset .infoNote{float:left; width:240px;}
.fieldset .diagram{float:right; width:650px;}
.chart{width:900px; margin:0 auto}
.clear20{clear:both; height:20px}
.clear10{clear:both; height:10px}
</style>
<!-- colum right -->
<div class="middle myclear">
	<div class="pad10">
		
		<h2 class="marbottom10 martop20">
			Report
		</h2>
	</div>
	
	<input type="hidden" id="hddSummaryTime" />
	
	
	<input type="hidden" id="hddGroupTime" />
	<input type="hidden" id="hddGroupFromDate" value="" />
	<input type="hidden" id="hddGroupToDate" value="" />
	<input type="hidden" id="hddDomainTime" />
	<input type="hidden" id="hddDomainFromDate" value="" />
	<input type="hidden" id="hddDomainToDate" value="" />
    <div id="tab" style="position: inherit">
    	<ul>
			<li><a href="#info" id="summary">Thống kê người dùng</a></li>
			<li><a href="#settingmail" id="group">Phân bổ theo nhóm</a></li>
            <li><a href="#domainreport" id="domain">Thống kê theo domain</a></li>
            <li><a href="#budgettab" id="budget">Báo cáo Tài chính</a></li>
            <li><a href="#bannerdefaulttab" id="bannerdefault">Banner Default</a></li>
            <li><a href="#cpcdomaintab" id="cpcdomain">CPC Domain</a></li>
            <li><a href="#boxmuachungtab" id="boxmuachung">Box Muachung</a></li>
		</ul>
        <div id="info">
        	<fieldset class="fieldset">
			    <legend>Thông tin chung</legend>
			    <?php if(!empty($data['data1'])){ ?>
			        <div class="sumaryItem"> 
			            &raquo; Tổng số người dùng: <span><?php echo($data['data1'][0]['_totaluser']); ?></span>
			        </div>
			        <div class="sumaryItem">
			            &raquo; Tổng số người dùng Active:<span><?php echo($data['data1'][0]['_activeuser']); ?></span>
			        </div>
			        <div class="sumaryItem">
			            &raquo; Tổng số người dùng chưa Active: <span><?php echo($data['data1'][0]['_unactiveuser']); ?></span>
			        </div>
			        <div class="sumaryItem">
			            &raquo; Tổng số người dùng nạp tiền: <span><?php echo($data['data1'][0]['_monneyuser']); ?></span>
			        </div>
			        <div class="sumaryItem">
			            &raquo; Tổng số người dùng tiêu tiền: <span><?php echo($data['data1'][0]['_spentuser']); ?></span>
			        </div>
			        <div class="sumaryItem">
			            &raquo; % Số tiền tiêu trên số tiền nạp: <span><?php echo($data['data1'][0]['_percentspent']); ?>%</span>
			        </div>
			        <div class="sumaryItem">
			            &raquo; % Số tiền khuyến mãi trên số tiền nạp: <span><?php echo($data['data1'][0]['_percentpromotion']); ?>%</span>
			        </div>
			    <?php }?>
			</fieldset>
			<div class="clear20"><span></span></div>
			<fieldset class="fieldset">
			    <legend>Tỷ lệ người dùng kích hoạt và chưa kích hoạt</legend>
			  	 <?php if(!empty($data['data1'])){ ?>
			     	 <div class="sumaryItem">
			             &raquo; Tổng số người dùng: <span><?php echo($data['data1'][0]['_totaluser']); ?></span>
			         </div>
			     <?php }?>
			    <div id="Active_Unactive" class="chart"></div>
			    
			    <div class="clear10"><span></span></div>
			</fieldset>
			<div class="clear20"><span></span></div>
			<fieldset class="fieldset">
			    <legend>Tỷ lệ người dùng nạp tiền/ người dùng Active</legend>
			     <?php if(!empty($data['data1'])){ ?>
			     	 <div class="sumaryItem">
			             &raquo; Tổng số người dùng kích hoạt: <span><?php echo($data['data1'][0]['_activeuser']); ?></span>
			         </div>
			     <?php }?>
			    <div id="Active_Money" class="chart"></div>
			    <div class="clear10"><span></span></div>
			</fieldset>
			<div class="clear20"><span></span></div>
			<fieldset class="fieldset">
			    <legend>Tỷ lệ người tiêu tiền/ người nạp tiền</legend>
			    <?php if(!empty($data['data1'])){ ?>                   
			        <div class="sumaryItem">
			            &raquo; Tổng số người dùng nạp tiền: <span><?php echo($data['data1'][0]['_monneyuser']); ?></span>
			        </div>
			        <div class="sumaryItem">
			            &raquo; Tổng số người dùng tiêu tiền: <span><?php echo($data['data1'][0]['_spentuser']); ?></span>
			        </div>
			    <?php }?>
			    <div id="Spent_Money" class="chart"></div>               
			    <div class="clear10"><span></span></div>
			</fieldset>
			<div class="clear20"><span></span></div>
        </div>
        <div id="settingmail">
        	<div class="dashboard-box myclear">
                <table class="mbd-timebox martop20" cellpadding="0" cellspacing="0" border="0">	
                    <tr>
                        <td class="padright10 ver-middle">
                            <input class="input-time" type="text" id="date" value="<?php echo date('d/m/Y',strtotime($fromdate))?> <?php echo $banner_lang['to'];?> <?php echo date('d/m/Y',strtotime($todate))?>" style="width: 220px;"/>
                        </td>
                        <td class="ver-middle">
                            <input type="button" class="btn" value="Xem" onclick="groupDateChange('<?php echo site_url('report/group_summary_ajax'); ?>');" />
                        </td>
                        <td class="padleft10 ver-middle">
                            <img id="loading" src="/images/ajaxLoading.gif" class="martop5 ver-middle" style="display: none;"/>
                        </td>
                    </tr>
                </table>
                <div id="campaigndiv">
                </div>	
            </div>
            <div id="chartView"></div>
        </div>
        <div id="domainreport">
        	<div class="dashboard-box myclear">
                <table class="mbd-timebox martop20" cellpadding="0" cellspacing="0" border="0">	
                    <tr>
                        <td class="padright10 ver-middle">
                            <input class="input-time" type="text" id="date1" value="<?php echo date('d/m/Y',strtotime($fromdate))?> <?php echo $banner_lang['to'];?> <?php echo date('d/m/Y',strtotime($todate))?>" style="width: 220px;"/>
                        </td>
                        <td class="padleft10 ver-middle">
                            <img id="loading1" src="/images/ajaxLoading.gif" class="martop5 ver-middle" style="display: none;"/>
                        </td>
                    </tr>
                </table>	
            </div>
            <div id="domainView"></div>
        </div>
        <div id="budgettab">
        	<div class="dashboard-box myclear">
                <table class="mbd-timebox martop20" cellpadding="0" cellspacing="0" border="0">	
                    <tr>
                        <td class="padright10 ver-middle">
                            <input class="input-time" type="text" id="date2" value="<?php echo date('d/m/Y',strtotime($fromdate))?> <?php echo $banner_lang['to'];?> <?php echo date('d/m/Y',strtotime($todate))?>" style="width: 220px;"/>
                        </td>
                        <td class="ver-middle">
                            <input type="button" class="btn" value="Xem" onclick="getBudget();" />
                        </td>
                        <td class="padleft10 ver-middle">
                            <img id="loading2" src="/images/ajaxLoading.gif" class="martop5 ver-middle" style="display: none;"/>
                        </td>
                    </tr>
                </table>	
            </div>
            <div id="budgetView"></div>
        </div>
        <div id="bannerdefaulttab">
        	<img src="/images/ajaxLoading.gif" class="loading" border="0" style="display: none"/>
        	<div id="bannerDefaultView"></div>
        </div>
        <div id="cpcdomaintab">
        	<img src="/images/ajaxLoading.gif" class="loading" border="0" style="display: none"/>
        	<div id="cpcDomainView"></div>
        </div>
        <div id="boxmuachungtab">
            <img src="/images/ajaxLoading.gif" class="loading" border="0" style="display: none"/>
        	<div id="boxMuachungView"></div>
        </div>
    </div>
</div>
<p class="text-right pad10">
    <img id="top" class="cursor-pointer" alt="" src="images/backtotop.gif" onclick="window.scrollTo(0,0); return false;"/>
</p>
<script type="text/javascript">
   var addCommasNumber=function addCommas(nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
   $(document).ready(function(){
		$('#tab').tabs({
			select: function(event, ui) {
				switch(ui.tab.id)
				{
					case 'summary':
						break;
					case 'group':
						groupDateChange('/admin/report/group_summary_ajax/');
						break;
					case 'domain':
						adDomainFilter();
						break;
					case 'budget':
						getBudget();
						break;
					case 'bannerdefault':
						bannerDefaultView();
						break;
					case 'cpcdomain':
						window.location = "<?php echo site_url('report/cpcdomain'); ?>";
						break;
                    case 'boxmuachung':
						window.location = "<?php echo site_url('report/boxmc'); ?>";
						break;
				}
			}
		});
		
		var chart1,chart2,chart3;
				
	   <?php if($active_unactive!=''){ ?>
	   chart1 = new Highcharts.Chart({
		  chart: {
			 renderTo: 'Active_Unactive',
			 plotBackgroundColor: null,
			 plotBorderWidth: null,
			 plotShadow: true
		  },
		  title: {
			 text: 'Tỷ lệ người dùng kích hoạt và chưa kích hoạt'
		  },
		  tooltip: {
			 formatter: function() {
				return '<b>'+ this.point.name +'</b>:  '+ addCommasNumber(this.point.vote)+' người, Chiếm:'+ this.y +' %';
			 }
		  },
		  plotOptions: {
			 pie: {
				allowPointSelect: true,
				cursor: 'pointer',
				dataLabels: {
				   enabled: true,
				   color: Highcharts.theme.textColor || '#000000',
				   connectorColor: Highcharts.theme.textColor || '#000000',
				   formatter: function() {
					  return '<b>'+ this.point.name +'</b>  '+ addCommasNumber(this.point.vote)+' người, - Chiếm '+ this.y +' %';
				   }
				},
				showInLegend: true
			 }
		  },
		   series: [{
			 type: 'pie',
			 name: '',
			 data: [
				<?php echo $active_unactive;?>
			 ]
		  }],
		  exporting: {
			enabled: false
		  }
	   });
	   <?php }else{?>
	   		$('#Active_Unactive').html('Chưa có dữ liệu trả về');
	   <?php }?>
	   <?php if($active_monney!=''){ ?>
	  //chart 2
	    chart2 = new Highcharts.Chart({
		  chart: {
			 renderTo: 'Active_Money',
			 plotBackgroundColor: null,
			 plotBorderWidth: null,
			 plotShadow: true
		  },
		  title: {
			 text: 'Tỷ lệ người dùng nạp tiền/ người dùng kích hoạt'
		  },
		  tooltip: {
			 formatter: function() {
				return '<b>'+ this.point.name +'</b>:  '+ addCommasNumber(this.point.vote)+' người, Chiếm:'+ this.y +' %';
			 }
		  },
		  plotOptions: {
			 pie: {
				allowPointSelect: true,
				cursor: 'pointer',
				dataLabels: {
				   enabled: true,
				   color: Highcharts.theme.textColor || '#000000',
				   connectorColor: Highcharts.theme.textColor || '#000000',
				   formatter: function() {
					  return '<b>'+ this.point.name +'</b>:  '+ addCommasNumber(this.point.vote)+' người, Chiếm:'+ this.y +' %';
				   }
				},
				showInLegend: true
			 }
		  },
		   series: [{
			 type: 'pie',
			 name: '',
			 data: [
			 		<?php echo $active_monney;?>
			 ]
		  }],
		  exporting: {
			enabled: false
		  }
	   });
	   <?php }else{?>
	  		$('#Active_Money').html('Chưa có dữ liệu trả về');
	   <?php }?>
	    <?php if($spent_monney!=''){ ?>
	   //chart 3
	    chart3 = new Highcharts.Chart({
		  chart: {
			 renderTo: 'Spent_Money',
			 plotBackgroundColor: null,
			 plotBorderWidth: null,
			 plotShadow: true
		  },
		  title: {
			 text: 'Tỷ lệ người tiêu tiền/ người nạp tiền'
		  },
		  tooltip: {
			 formatter: function() {
				return '<b>'+ this.point.name +'</b>:  '+ addCommasNumber(this.point.vote)+' người, - Chiếm '+ this.y +' %';
			 }
		  },
		  plotOptions: {
			 pie: {
				allowPointSelect: true,
				cursor: 'pointer',
				dataLabels: {
				   enabled: true,
				   color: Highcharts.theme.textColor || '#000000',
				   connectorColor: Highcharts.theme.textColor || '#000000',
				   formatter: function() {
					  return '<b>'+ this.point.name +'</b>:  '+ addCommasNumber(this.point.vote)+' người, - Chiếm '+ this.y +' %';
				   }
				},
				showInLegend: true
			 }
		  },
		   series: [{
			 type: 'pie',
			 name: '',
			 data: [
			 		<?php echo $spent_monney;?>
			 ]
		  }],
		  exporting: {
			enabled: false
		  }
	   });
	   <?php }else{?>
	   		$('#Spent_Money').html('Chưa có dữ liệu trả về');
	   <?php }?>
	   
	  $('.sumaryItem').find('span').each(function(){
	  	$(this).html(addCommasNumber($(this).html()));
	  });
	  $('.price').each(function(){
	  	$(this).html(addCommasNumber($(this).html()));
	  });
		
	});
		
	function groupDateChange(url)
    {
    	var strDate = $('#date').val();
    	var arrDate = strDate.split(' ');
    	var arrStartDate = $.trim(arrDate[0]).split('/');
    	var strStartDate = arrStartDate[2] + '-' + arrStartDate[1] + '-' + arrStartDate[0];
    	
    	var arrEndDate = $.trim(arrDate[2]).split('/');
    	var strEndDate = arrEndDate[2] + '-' + arrEndDate[1] + '-' + arrEndDate[0];
    	
    	var currFromDate = $.trim($('#hddGroupFromDate').val());
    	var currToDate = $.trim($('#hddGroupToDate').val());
    	
    	arrDate = null;
		arrStartDate = null;
		arrEndDate = null;
		var currTime = (new Date()).getTime();
		var hddTime = $.trim($('#hddGroupTime').val()) == '' ? 0 : parseFloat($.trim($('#hddGroupTime').val()));
		var tmpTime = (currTime - hddTime) / 1000; // tinh theo giay
		
    	if(strStartDate != currFromDate || strEndDate != currToDate || (tmpTime > 600))
    	{
    		$('#hddGroupTime').val(currTime);
    		$('#hddGroupFromDate').val(strStartDate);
    		$('#hddGroupToDate').val(strEndDate);
    		var _onSuccess = function(data)
	    	{
	    		$('#loading').hide();
	    		if($.trim(data) == 'login')
				{
					window.location.reload(true);
					return;
				}
				else if($.trim(data) == 'notright')
				{
					alert('Bạn không có quyền thực hiện chức năng này.');
					window.location = '/admin/logout';
					return;
				}
				else
				{
					$('#campaigndiv').html(data);
				}
	    	};
	    	var _onError = function(data)
	    	{
	    		$('#campaigndiv').html('Error!');
	     		$('#loading').hide();
	    	};
	    	$('#loading').show();
			getAjax(url,'fromdate=' + strStartDate + '&todate=' + strEndDate, '', '', '', false, _onSuccess, _onError);
    	}
    }
    
    function adDomainFilter(domain)
    {
        var url = '<?php echo site_url('report/domain'); ?>';
    	var strDate = $('#date1').val();
    	var arrDate = strDate.split(' ');
    	var arrStartDate = $.trim(arrDate[0]).split('/');
    	var strStartDate = arrStartDate[2] + '-' + arrStartDate[1] + '-' + arrStartDate[0];
    	
    	var arrEndDate = $.trim(arrDate[2]).split('/');
    	var strEndDate = arrEndDate[2] + '-' + arrEndDate[1] + '-' + arrEndDate[0];
    	
    	var currFromDate = $.trim($('#hddDomainFromDate').val());
    	var currToDate = $.trim($('#hddDomainToDate').val());
    	if(typeof(domain) == 'undefined')
    	{
	    	var domain = $('#domain_view').val();
	    	if(domain == '' || domain == 'undefined' || domain == null) domain = '';
    	}
    		
    		var _onSuccess = function(data)
	    	{
	    		$('#loading1').hide();
	    		if($.trim(data) == 'login')
				{
					window.location.reload(true);
					return;
				}
				else if($.trim(data) == 'notright')
				{
					alert('Bạn không có quyền thực hiện chức năng này.');
					window.location = '/admin/logout';
					return;
				}
				else
				{
					$('#domainView').html(data);
				}
				
	    	};
	    	var _onError = function(data)
	    	{
	    		$('#domainView').html('Error!');
	     		$('#loading1').show();
	    	};
	    	$('#loading1').show();
	    	getAjax(url,'fromdate=' + strStartDate + '&todate=' + strEndDate + '&domain=' + domain, '', '', '', false, _onSuccess, _onError);
    }
	
	$(document).ready(function(){
		$('#date').DatePicker({
			format: 'd/m/Y',
			date: ['<?php echo date('d/m/Y',strtotime($fromdate));?>','<?php echo date('d/m/Y',strtotime($todate));?>'],
			current: '<?php echo date('d/m/Y');?>',
			calendars: 2,
			mode: 'range',
			starts: 1, 
			onChange: function(formated, dates) 
			{
				var str = formated.join(' <?php echo $banner_lang['to'];?> ');
				$('#date').val(str);
				$('#date').DatePickerHide();
				
				$('#date1, #date2').val(str);
			}
		});
        $('#date1').DatePicker({
			format: 'd/m/Y',
			date: ['<?php echo date('d/m/Y',strtotime($fromdate));?>','<?php echo date('d/m/Y',strtotime($todate));?>'],
			current: '<?php echo date('d/m/Y');?>',
			calendars: 2,
			mode: 'range',
			starts: 1, 
			onChange: function(formated, dates) 
			{
				var str = formated.join(' <?php echo $banner_lang['to'];?> ');
				$('#date1').val(str);
				$('#date1').DatePickerHide();
				
				$('#date, #date2').val(str);
				adDomainFilter();
			}
		});
        $('#date2').DatePicker({
			format: 'd/m/Y',
			date: ['<?php echo date('d/m/Y',strtotime($fromdate));?>','<?php echo date('d/m/Y',strtotime($todate));?>'],
			current: '<?php echo date('d/m/Y');?>',
			calendars: 2,
			mode: 'range',
			starts: 1, 
			onChange: function(formated, dates) 
			{
				var str = formated.join(' <?php echo $banner_lang['to'];?> ');
				$('#date2').val(str);
				$('#date2').DatePickerHide();
				$('#date, #date1').val(str);
			}
		});
	});
function showGroup(type)
{
   var dt = $('#date').val().split(' ');
   var fromdate = dt[0].split('/');
   var todate = dt[2].split('/');
   fromdate = fromdate[2] + '-' + fromdate[1] + '-' + fromdate[0];
   todate = todate[2] + '-' + todate[1] + '-' + todate[0];
   $('#loading').show();
	if(type == -1)
	{
		$.ajax({
			url: '<?php echo site_url('report/group_summary_ajax')?>/?fromdate=' + fromdate + '&todate=' + todate,
			success: function(data){
				$('#loading').hide();
				$('#campaigndiv').html(data);
			},
			error: function(data){
				$('#loading').hide();
				$('#campaigndiv').html('Error');
			}
		});
	}
	else
	{
		$.ajax({
			url: '<?php echo site_url('report/group_summary_ajax')?>/?type=' + type + '&fromdate=' + fromdate + '&todate=' + todate,
			success: function(data){
				$('#loading').hide();
				$('#campaigndiv').html(data);
			},
			error: function(data){
				$('#loading').hide();
				$('#campaigndiv').html('Error');
			}
		});
	}
}
	   
function getBudget(type)
{
	var dt = $('#date2').val().split(' ');
	var fromdate = dt[0].split('/');
	var todate = dt[2].split('/');
	var budgetType = $('#budget_type').val();
	fromdate = fromdate[2] + '-' + fromdate[1] + '-' + fromdate[0];
	todate = todate[2] + '-' + todate[1] + '-' + todate[0];
	if(typeof(type) == 'undefined')
	{
		if(budgetType == '' || budgetType == 'undefined' || budgetType == null)
		{
			budgetType = -1;
		}
	}
	else budgetType = type;

	//Kiem tra action de lay du lieu
	var link = '';
	if(budgetType == -1)
		link = '<?php echo site_url('report/budget');?>/?fd=' + fromdate + '&td=' + todate;
	else
		link = '<?php echo site_url('report/budgetagency');?>/?gid=' + budgetType + '&fd=' + fromdate + '&td=' + todate;
	
	$('#loading2').show();
	$.ajax({
		url: link,
		success: function(data){
			$('#loading2').hide();
			$('#budgetView').html(data);
		}
	});
}

function bannerDefaultView()
{
	$strPage = (arguments[0] ? '/?page=' + arguments[0] : '');
	
	$obj = $('#bannerDefaultView');
	$img = $('#bannerdefaulttab img.loading');
	$img.show();
	$.ajax({
		url: '<?php echo site_url('report/ajaxBannerDefaultReport');?>' + $strPage,
		success: function(data){
			$img.hide();
			$obj.html(data);
		}
	});
}
function boxMuachungView()
{
	$strPage = (arguments[0] ? '/?page=' + arguments[0] : '');
	
	$obj = $('#cpcBoxMuaChungView');
	$img = $('#loading3');
	$img.show();
	$.ajax({
		url: '<?php echo site_url('report/ajaxBoxMuachungReport');?>' + $strPage,
		success: function(data){
			$img.hide();
			$obj.html(data);
		}
	});
}
</script>